//
//  Utilities.m
//  QMovie
//  Created by liurg on 15/8/6.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "Utilities.h"


@implementation Utilities

+(NSString *)bundlePath:(NSString *)fileName {
    return [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:fileName];
}

+(NSString *)documentsPath:(NSString *)fileName {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:fileName];
}


+ (BOOL)createVideoFolderIfNotExist
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path =[NSString stringWithFormat:@"%@/hebing/",documentsDirectory];
    
    
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = FALSE;
    BOOL isDirExist = [fileManager fileExistsAtPath:path isDirectory:&isDir];
    
    if(!(isDirExist && isDir))
    {
        BOOL bCreateDir = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
        if(!bCreateDir){
            DLog(@"创建图片文件夹失败");
            return NO;
        }
        return YES;
    }
    return YES;
}



+(NSString *)CreateDocumentsFilePathName:(NSString *)fileName {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    NSString *path =[NSString stringWithFormat:@"%@/hebing/",documentsDirectory];
    
    path = [path stringByAppendingPathComponent:fileName];
    
    return path;
}

-(void)readFileFromDocuments:(NSString *)fileName{
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,                                                                          NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"fileName"];
    
    NSArray *array = [fileManager directoryContentsAtPath:filePath];
    
    
}


+ (BOOL)createFolderIfNotExist:(NSString *)fileName
{
    // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //  NSString *path =[NSString stringWithFormat:@"%@/tmp/",NSHomeDirectory()];
    
    //[paths objectAtIndex:0];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path =[NSString stringWithFormat:@"%@/%@/",documentsDirectory,fileName];
    
    
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = FALSE;
    BOOL isDirExist = [fileManager fileExistsAtPath:path isDirectory:&isDir];
    
    if(!(isDirExist && isDir))
    {
        BOOL bCreateDir = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
        if(!bCreateDir){
            DLog(@"创建图片文件夹失败");
            return NO;
        }
        return YES;
    }
    return YES;
}


+(NSString *)getSaveGifFilePathString{
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path =[NSString stringWithFormat:@"%@/%@/",documentsDirectory,@"gifPicture"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = FALSE;
    BOOL isDirExist = [fileManager fileExistsAtPath:path isDirectory:&isDir];
    
    if(!(isDirExist && isDir))
    {
        BOOL bCreateDir = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
        if(!bCreateDir){
            DLog(@"创建图片文件夹失败");
        }
    }
    return path;
}

+(NSArray *)getGifPictureFromPath{
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path =[NSString stringWithFormat:@"%@/%@/",documentsDirectory,@"gifPicture"];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSArray *array = [fileManager contentsOfDirectoryAtPath:path error:NULL];
    
    return array;
    
    
}

+(void)deleteGifPictureInPath{
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path =[NSString stringWithFormat:@"%@/%@/",documentsDirectory,@"gifPicture"];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    [fileManager removeItemAtPath:path error:nil];
    
}


+ (NSString *)getSaveFilePathString
{
    // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //  NSString *path = [paths objectAtIndex:0];
    
    //  NSString *path =[NSString stringWithFormat:@"%@/picture/",NSHomeDirectory()];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path =[NSString stringWithFormat:@"%@/picture/",documentsDirectory];
    return path;
    
    
}

+ (NSArray *)getPictureFilePath
{
    // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //  NSString *path = [paths objectAtIndex:0];
    
    //  NSString *path =[NSString stringWithFormat:@"%@/picture/",NSHomeDirectory()];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path =[NSString stringWithFormat:@"%@/picture/",documentsDirectory];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    
    NSArray *array = [fileManager contentsOfDirectoryAtPath:path error:NULL];
    
    return array;
    
}


+ (NSArray *)getPictureFilePathForPlay
{
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path =[NSString stringWithFormat:@"%@/picture/",documentsDirectory];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSString *pathForPlay =[NSString stringWithFormat:@"%@/playPicture",documentsDirectory];
    
    BOOL isDir = FALSE;
    BOOL isDirExist = [fileManager fileExistsAtPath:pathForPlay isDirectory:&isDir];
    
    if(!(isDirExist && isDir))
    {
        BOOL bCreateDir = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
        if(!bCreateDir){
            DLog(@"创建图片文件夹失败");
            return NO;
        }
    }
    
    [fileManager removeItemAtPath:pathForPlay error:nil];
    [fileManager copyItemAtPath:path toPath:pathForPlay error:NULL];
    NSArray *array = [fileManager contentsOfDirectoryAtPath:pathForPlay error:NULL];
    
    return array;
    
    
}


+(void)deletePictureFileDocuments{
    
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path =[NSString stringWithFormat:@"%@/picture/",documentsDirectory];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    [fileManager removeItemAtPath:path error:nil];
    
}

+ (void)deleteDocmentsFile:(NSString *)fileStr {
    
    if (fileStr) {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        [fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
        NSError *err = nil;
        if ([fileManager fileExistsAtPath:fileStr]) {
            [fileManager removeItemAtPath:fileStr error:&err];
        }
        
        //        if (err)
        //            return NO;
        //        else
        //            return YES;
    }
    
    // return NO;
}

+ (void)deleteTempFile:(NSString *)fileStr {
    
    if (fileStr) {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:2];
        [fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
        NSError *err = nil;
        if ([fileManager fileExistsAtPath:fileStr]) {
            [fileManager removeItemAtPath:fileStr error:&err];
        }
    }
}


// 写文件(若存在，则删旧文件)
+ (BOOL)operatingWriteFile:(NSString *)fileStr NeedData:(NSData *)theData {
    
    if (fileStr && theData && theData.length>0) {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        [fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
        NSError *err = nil;
        if ([fileManager fileExistsAtPath:fileStr]) {
            [fileManager removeItemAtPath:fileStr error:&err];
        }
        
        if (err)
            return NO;
        
        return [fileManager createFileAtPath:fileStr contents:theData attributes:nil];
    }
    
    return NO;
}


//*****************************************************************************//



+ (NSArray *)getDataFromFilePath:(NSString *)FilePath
{
    
    //    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //    NSString *documentsDirectory = [paths objectAtIndex:0];
    //    NSString *path =[NSString stringWithFormat:@"%@/picture/",documentsDirectory];
    //    NSString *pathForPlay =[NSString stringWithFormat:@"%@/playPicture",documentsDirectory];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    BOOL isDir = FALSE;
    BOOL isDirExist = [fileManager fileExistsAtPath:FilePath isDirectory:&isDir];
    
    if(!(isDirExist && isDir))
    {
        BOOL bCreateDir = [fileManager createDirectoryAtPath:FilePath withIntermediateDirectories:YES attributes:nil error:nil];
        if(!bCreateDir){
            DLog(@"创建图片文件夹失败");
            return NO;
        }
    }
    
    NSArray *array = [fileManager contentsOfDirectoryAtPath:FilePath error:NULL];
    
    return array;
    
    
}



// 创建文件夹

+ (BOOL)createDocmentFolderIfNotExist:(NSString *)filePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path =[NSString stringWithFormat:@"%@/%@/",documentsDirectory,filePath];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = FALSE;
    BOOL isDirExist = [fileManager fileExistsAtPath:path isDirectory:&isDir];
    
    if(!(isDirExist && isDir))
    {
        BOOL bCreateDir = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
        if(!bCreateDir){
            DLog(@"创建图片文件夹失败");
            return NO;
        }
        return YES;
    }
    return YES;
}

+ (NSString *)getDocmentSaveFilePathString:(NSString *)fileName filePath:(NSString *)filePath
{
    [Utilities createDocmentFolderIfNotExist:filePath];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path =[NSString stringWithFormat:@"%@/%@/",documentsDirectory,filePath];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    formatter.dateFormat = @"yyyyMMddHHmm";
    NSString *nowTimeStr = [formatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:0]];
    NSString *filePathName = [[path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-%@",nowTimeStr,[fileName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]] stringByAppendingString:@".mp3"];
    
    // NSString *fileName = [[path stringByAppendingPathComponent:videoName] stringByAppendingString:@".mp4"];
    DLog(@"filePathName :%@",filePathName);
    
    return filePathName;
    
}

// 创建文件夹
+ (BOOL)createTempFolderIfNotExist:(NSString *)pathFile
{
    // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path =[NSString stringWithFormat:@"%@/tmp/",NSHomeDirectory()];
    NSString *folderPath = [path stringByAppendingPathComponent:pathFile];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = FALSE;
    BOOL isDirExist = [fileManager fileExistsAtPath:folderPath isDirectory:&isDir];
    
    if(!(isDirExist && isDir))
    {
        BOOL bCreateDir = [fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
        if(!bCreateDir){
            DLog(@"创建图片文件夹失败");
            return NO;
        }
        return YES;
    }
    return YES;
}


+ (NSString *)getTempSaveFilePathString:(NSString *)fileName filePath:(NSString *)filePath
{
    [Utilities createTempFolderIfNotExist:filePath];
    
    NSString *path =[NSString stringWithFormat:@"%@/tmp/",NSHomeDirectory()];
    path = [path stringByAppendingPathComponent:filePath];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    formatter.dateFormat = @"yyyyMMddHHmmss";
    
    NSString *nowTimeStr = [formatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:0]];
    
    NSString *filePathName = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-%@",nowTimeStr,[fileName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    
    // NSString *fileName = [[path stringByAppendingPathComponent:videoName] stringByAppendingString:@".mp4"];
    DLog(@"filePathName :%@",filePathName);
    
    return filePathName;
    
}


+(void)deleteFolderDocuments:(NSString *)FolderPath{
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path =[NSString stringWithFormat:@"%@/%@/",documentsDirectory,FolderPath];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:path error:nil];
}

+(void)deleteFolderTemp:(NSString *)FolderPath{
    
    NSString *path =[NSString stringWithFormat:@"%@/tmp/",NSHomeDirectory()];
    NSString *folderPath = [path stringByAppendingPathComponent:FolderPath];
    
    // NSString *path =[NSString stringWithFormat:@"%@/%@/",documentsDirectory,FolderPath];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:folderPath error:nil];
    
}




@end
